package com.zh.note.huawei.onehundred.磁盘容量排序;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

/**
 * @Author: zhanghuan
 * @date: 2023/9/1 09:40
 * @description: create it
 * 难点：稳定排序，相对位置不变
 * 1G在1024M前面
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        String[] strs = new String[n];
        for (int i = 0; i < n; i++) {
            strs[i] = sc.next();
        }
        Arrays.sort(strs, Comparator.comparingInt(Main::calc));
        Arrays.stream(strs).forEach(System.out::println);
    }

    private static int calc(String str) {
        int sum = 0;
        int index = -1;
        for (int j = 0; j < str.length(); j++) {
            if (str.charAt(j) == 'M') {
                String substring = str.substring(index + 1, j);
                sum += Integer.parseInt(substring);
                index = j;
            } else if (str.charAt(j) == 'G') {
                String substring = str.substring(index + 1, j);
                sum += Integer.parseInt(substring) * 1024;
                index = j;
            } else if (str.charAt(j) == 'T') {
                String substring = str.substring(index + 1, j);
                sum += Integer.parseInt(substring) * 1024 * 1024;
                index = j;
            }
        }
        return sum;
    }
}
